package com.ibeeking.found.upms.g.rest.controller.v1;

import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.constants.HeaderNounConstant;
import com.ibeeking.found.common.db.annotation.IgnoreDS;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.common.entity.LoginUserDTO;
import com.ibeeking.found.common.enums.member.MemberSexEnum;
import com.ibeeking.found.upms.api.common.query.AccountLoginQuery;
import com.ibeeking.found.upms.g.rest.service.IGmcUserService;
import com.ibeeking.found.upms.service.common.param.GmcUserParam;
import com.ibeeking.found.upms.service.common.param.ModifyMobileParam;
import com.ibeeking.found.upms.service.common.param.UserFindPwdParam;
import com.ibeeking.found.upms.service.common.param.UserModifyPwdParam;
import com.ibeeking.found.upms.service.common.query.GmcUserPageQuery;
import com.ibeeking.found.upms.service.common.vo.CurrentPermissionVO;
import com.ibeeking.found.upms.service.common.vo.CurrentUserVO;
import com.ibeeking.found.upms.service.common.vo.GmcUserPageVO;
import com.ibeeking.found.upms.service.common.vo.GmcUserVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @ClassName UserController控制器
 * @Description 全局用户管理
 * @Author ibeeking
 * @Date 2020-12-02 15:29
 **/
@RestController
@RequestMapping("/g/user/v1")
public class GmcUserController {

    @Resource
    private IGmcUserService userServiceImpl;

   // @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取当前登录用户权限菜单")
    @GetMapping(value = "/currentPermission")
    public List<CurrentPermissionVO> currentPermission(@LoginUser(isFull = true) CurrentUser currentUser, HttpServletRequest request) {
        return userServiceImpl.currentPermission(currentUser, request.getHeader(HeaderNounConstant.JWT_JTI));
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取当前登录用户信息")
    @GetMapping(value = "/currentInfo")
    public CurrentUserVO currentPermission(@LoginUser(isFull = true) CurrentUser currentUser) {
        CurrentUserVO currentUserVO = BeanUtil.convertBean(currentUser, CurrentUserVO.class);
        currentUserVO.setSexName(MemberSexEnum.getDescByCode(currentUserVO.getSex()));
        return currentUserVO;
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "分页查询全局用户列表")
    @PostMapping(value = "/pageList")
    public Page<GmcUserPageVO> pageList(@RequestBody GmcUserPageQuery gmcUserPageQuery) {
        return userServiceImpl.pageList(gmcUserPageQuery);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "新增全局用户")
    @PostMapping(value = "/add")
    public Boolean add(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Insert.class) GmcUserParam gmcUserParam) {
        gmcUserParam.setCreateBy(currentUser.getId());
        return userServiceImpl.add(gmcUserParam);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改全局用户")
    @PostMapping(value = "/modify")
    public Boolean modify(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Update.class) GmcUserParam gmcUserParam) {
        gmcUserParam.setModifyBy(currentUser.getId());
        return userServiceImpl.modify(gmcUserParam);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询全局用户信息")
    @GetMapping(value = "/view/{id}")
    public GmcUserVO view(@PathVariable("id") Long id) {
        return userServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "启用/禁用全局用户")
    @GetMapping(value = "/changeStatus/{id}/{status}")
    public Boolean changeStatus(@LoginUser CurrentUser currentUser, @PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return userServiceImpl.changeStatus(id, status, currentUser.getId());
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "删除全局用户")
    @GetMapping(value = "/del/{id}")
    public Boolean del(@PathVariable("id") Long id) {
        return userServiceImpl.del(id);
    }

    /**
     * 重置密码
     *
     * @param id
     * @return
     */
    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "重置密码")
    @GetMapping(value = "/resetPwd/{id}")
    public Boolean resetPwd(@PathVariable("id") Long id) {
        return userServiceImpl.resetPwd(id);
    }

    /**
     * 修改密码
     *
     * @param currentUser
     * @param userModifyPwdParam
     * @return
     */
    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改密码")
    @PostMapping(value = "/modifyPwd")
    public Boolean modifyPwd(@LoginUser CurrentUser currentUser, @RequestBody @Validated UserModifyPwdParam userModifyPwdParam) {
        return userServiceImpl.modifyPwd(currentUser.getId(), userModifyPwdParam);
    }

    /**
     * 找回密码
     *
     * @param userFindPwdParam
     * @return
     */
    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "找回密码")
    @PostMapping(value = "/findPwd")
    public Boolean findPwd(@RequestBody @Validated UserFindPwdParam userFindPwdParam) {
        return userServiceImpl.findPwd(userFindPwdParam);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询用户角色id列表")
    @GetMapping(value = "/queryRoleIdsByUserId/{userId}")
    public List<String> queryRoleIdsByUserId(@PathVariable("userId") @NotNull(message = "userId不能为空") Long userId) {
        return userServiceImpl.queryRoleIdsByUserId(userId);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询是否存在账号")
    @IgnoreDS
    @GetMapping(value = "/queryAccount/{mobile}")
    public Boolean del(@PathVariable("mobile") @NotBlank(message = "手机号必填") String mobile) {
        AccountLoginQuery query = new AccountLoginQuery();
        query.setMobile(mobile);
        LoginUserDTO loginUserDTO = userServiceImpl.queryAccountLogin(query);
        return null != loginUserDTO;
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改手机号")
    @PostMapping(value = "/modifyMobile")
    public Boolean modifyMobile(@LoginUser CurrentUser currentUser, @RequestBody @Validated ModifyMobileParam param) {
        return userServiceImpl.modifyMobile(currentUser.getId(), param);
    }
}
